﻿using System.Collections.Generic;
using Medianamik.Core.DAL;
using Medianamik.Core.DAL.Repositories;
using Medianamik.Core.DAL.Services;
using Medianamik.Core.Redirect;
using System.Linq;

namespace Medianamik.Client.Data.Repositories
{
    public interface IRedirectRepository : IMedianamikRepository<MedianamikRedirect>
    {

    }

    public class RedirectRepository : MedianamikRepository<MedianamikRedirectCollection,
        MedianamikRedirect>, IRedirectRepository, IRedirectUrlRepository
    {
        public RedirectRepository(ICultureService cultureService,
            IContentStateService contentStateService, INodeMapper nodeMapper)
            : base(cultureService, contentStateService, nodeMapper)
        {
        }

        #region Implementation of IRedirectUrlRepository

        public IList<Redirect> GetAllRedirect()
        {
            return AllInstances().Select(i => new Redirect
                {
                    Culture = i.Culture,
                    QueryString = i.QueryString,
                    RedirectFrom = i.RedirectFrom,
                    RedirectToNodeId = i.RedirectTo.GetValueOrDefault(),
                    ResponseCode = i.RedirectCode.GetValueOrDefault(302)
                }).ToList();
        }

        #endregion
    }
}
